package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.LockMode;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Flutter;
import com.hongkuncheng.cms.model.FlutterDirection;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 漂浮窗口
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/flutter")
public class AflutterController extends BaseController {

    // 漂浮窗口列表
    @RequestMapping
    public String index() {
        List<Flutter> flutters = factorySession.createCriteria(Flutter.class).list();
        request.setAttribute("flutters", flutters);
        return view();
    }

    // 漂浮窗口编辑
    @RequestMapping("edit")
    public String edit() {
        int flutterId = request.getParameter("flutterid") == null ? 0 :
                Integer.parseInt(request.getParameter("flutterid"));
        Flutter flutter = flutterId == 0 ? new Flutter() : factorySession.get(Flutter.class, flutterId);
        if (flutterId == 0) {
            FlutterDirection flutterdirection = factorySession.load(FlutterDirection.class, 2);
            flutter.setFlutterDirection(flutterdirection);
        }
        request.setAttribute("flutter", flutter);
        List<FlutterDirection> flutterdirections = factorySession.createCriteria(FlutterDirection.class)
                .list();
        request.setAttribute("flutterdirections", flutterdirections);
        return view();
    }

    // 漂浮窗口编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_plugin_flutterwindow");
        int flutterId = Integer.parseInt(request.getParameter("flutterid"));
        Flutter flutter = flutterId == 0 ? new Flutter() :
                factorySession.load(Flutter.class, flutterId, LockMode.PESSIMISTIC_WRITE);
        flutter.setImage(request.getParameter("image"));
        flutter.setStartX(Integer.parseInt(request.getParameter("start_x")));
        flutter.setStartY(Integer.parseInt(request.getParameter("start_y")));
        int flutterdirectionId = Integer.parseInt(request.getParameter("flutterdirectionid"));
        FlutterDirection flutterdirection = factorySession.load(FlutterDirection.class, flutterdirectionId);
        flutter.setFlutterDirection(flutterdirection);
        flutter.setUrl(request.getParameter("url"));
        if (flutterId == 0) factorySession.save(flutter);
        recordLog((flutterId == 0 ? "新建" : "更新") + "漂浮窗口");
        PageHelper.alertAndRedirect("保存成功", "./");
    }

    // 漂浮窗口删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_plugin_flutterwindow");
        int flutterId = Integer.parseInt(request.getParameter("flutterid"));
        Flutter flutter = factorySession.load(Flutter.class, flutterId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(flutter);
        recordLog("删除漂浮窗口");
        PageHelper.alertAndRedirect("删除成功", "./");
    }

}
